package com.fyh.controller;

import com.fyh.entity.common.Result;
import com.fyh.entity.job.DeliveryRecord;
import com.fyh.entity.job.JobPosition;
import com.fyh.entity.job.SimpleJobPosition;
import com.fyh.entity.user.SimpleCandidate;
import com.fyh.entity.user.Vitae;
import com.fyh.service.EmployerService;
import io.seata.spring.annotation.GlobalTransactional;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employer")
@Slf4j
public class EmployerController {

    @Autowired
    EmployerService employerService;

    /**
     * 获取职位列表
     * @return
     */
    @GetMapping("/jobList")
    public Result<List<SimpleJobPosition>> getJobPositionListByEmployerId(HttpServletRequest request) {
        Long employerId = Long.parseLong(request.getHeader("userId"));
        log.info("获取职位列表：{}", employerId);
        List<SimpleJobPosition> simpleJobPositions = employerService.getSimpleJobPositionListByEmployerId(employerId);
        return Result.success(simpleJobPositions);
    }

    /**
     * 获取职位详情
     * @return
     */
    @GetMapping("/jobDetail")
    public Result<JobPosition> getJobPositionDetail(@RequestParam("positionId") Long positionId) {
        log.info("获取职位详情：{}", positionId);
        JobPosition jobPosition = employerService.getJobPositionDetail(positionId);
        return Result.success(jobPosition);
    }

    /**
     * 更新职位
     * @return
     */
    @GlobalTransactional
    @PostMapping("/updateJobPosition")
    public Result updateJobPosition(@RequestBody JobPosition jobPosition) {
        log.info("更新职位:{}", jobPosition);
        employerService.updateJobPosition(jobPosition);
        return Result.success();
    }

    /**
     * 新增职位
     * @return
     */
    @GlobalTransactional
    @PostMapping("/insertJobPosition")
    public Result InsertJobPosition(@RequestBody JobPosition jobPosition, HttpServletRequest request) {
        log.info("新增职位:{}", jobPosition);
        jobPosition.setEmployerId(Long.parseLong(request.getHeader("userId")));
        employerService.addJobPosition(jobPosition);
        return Result.success();
    }

    /**
     * 删除职位
     * @return
     */
    @GlobalTransactional
    @DeleteMapping("/deleteJobPosition")
    public Result deleteJobPosition(@RequestParam("positionId") Long positionId){
        log.info("职位Id{} 删除职位", positionId);
        employerService.deleteJobPosition(positionId);
        return Result.success();
    }

    /**
     * 更新职位状态
     * @return
     */
    @PostMapping("/updateJobStatus/{positionId}")
    public Result updateJobStatus(@RequestBody Integer status, @PathVariable("positionId") Long positionId){
        log.info("职位Id{} 更新职位状态:{}", positionId, status);
        employerService.updateJobStatus(status, positionId);
        return Result.success();
    }

    /**
     * 获取职位投递者列表
     * @return
     */
    @GetMapping("/getCandidateList")
    public Result getCandidateList(@RequestParam("positionId") Long positionId, @RequestParam("status") String status){
        log.info("职位Id{} 获取职位下所有投递者", positionId);
        List<SimpleCandidate> candidateList = employerService.getSimpleCandidateList(positionId, status);
        return Result.success(candidateList);
    }

    /**
     * 获取简历详细信息
     * @return
     */
    @GetMapping("/getVitaeDetail")
    public Result<Vitae> getVitaeDetail(@RequestParam("candidateId") Long candidateId) {
        log.info("获取简历详细信息{}", candidateId);
        Vitae vitae = employerService.getVitaeDetail(candidateId);
        return Result.success(vitae);
    }

    /**
     * 更新职位状态
     * @return
     */
    @GlobalTransactional
    @PostMapping("/updateDeliveryStatus")
    public Result updateDeliveryStatus(@RequestBody DeliveryRecord deliveryRecord) {
        log.info("更新职位{}下的求职者{}投递状态{}", deliveryRecord.getPositionId(), deliveryRecord.getUserId(), deliveryRecord.getStatus());
        employerService.updateDeliveryStatus(deliveryRecord);
        return Result.success();
    }

    /**
     * 点击感兴趣后添加应聘者
     * @return
     */
    @PutMapping("/addContact")
    public Result addContact(@RequestParam("candidateId") Long contactId, @RequestHeader("userId") Long userId) {
        log.info("添加应聘者:{}", contactId);
        employerService.addChatContact(userId, contactId);
        return Result.success();
    }
}
